package main

import (
	"context"
	"flag"
	"fmt"
	"google.golang.org/api/option"
	"log"
	"os"

	language "cloud.google.com/go/language/apiv1"
	languagepb "google.golang.org/genproto/googleapis/cloud/language/v1"
)

var authJsonFile = flag.String("f", "app.json", "APP认证服务的json")

func main() {
	flag.Parse()

	file, err2 := os.OpenFile("test.txt", os.O_RDONLY|os.O_APPEND|os.O_CREATE, 0666)
	defer file.Close()

	if err2 != nil {
		panic(err2)
	}

	ctx := context.Background()

	// Creates a client.
	client, err := language.NewClient(ctx, option.WithCredentialsFile(*authJsonFile))
	if err != nil {
		log.Fatalf("Failed to create client: %v", err)
	}

	entities, err := client.AnalyzeEntities(ctx, &languagepb.AnalyzeEntitiesRequest{
		Document: &languagepb.Document{
			Type: languagepb.Document_PLAIN_TEXT,
			Source: &languagepb.Document_Content{
				Content: `【中信证券：恒生指数2020年上涨空间10%】美港电讯20日讯，中信证券报告表示，预计恒生指数和MSCI中国指数2020年上涨空间分别为10%和15%，盈利增速有望从底部回升，看好高分红和高股息率板块、消费和科技股。`,
			},
			Language: "zh",
		},
		EncodingType: languagepb.EncodingType_UTF8,
	})

	if err != nil {
		log.Fatalln(err)
	}

	for _, v := range entities.Entities {
		fmt.Fprintln(file, v.String())
	}
	log.Println(entities.String())
	//
	//classifyText, err := client.ClassifyText(ctx, &languagepb.ClassifyTextRequest{
	//	Document: &languagepb.Document{
	//		Type: languagepb.Document_PLAIN_TEXT,
	//		Source: &languagepb.Document_Content{
	//			Content: `[CITIC Securities: Hang Seng Index Up 10% in 2020] US-Hong Kong Telecom 20th, CITIC Securities Report said that the Hang Seng Index and MSCI China Index are expected to rise 10% and 15% in 2020, and earnings growth is expected to start from the bottom Rising, bullish on high dividends and high dividend yield sectors, consumer and technology stocks.`,
	//		},
	//	},
	//})
	//
	//if err != nil {
	//	log.Fatalf("Failed to classify text: %v", err)
	//}
	//
	//fmt.Println(classifyText.String())
}
